package com.huarui.user.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.codehaus.groovy.reflection.handlegen;
import org.ewaf.framework.action.BaseAction;

import com.huarui.company.model.Company;
import com.huarui.receive.action.ReceivesAction;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.user.model.User;
import com.huarui.user.service.UserService;
import com.huarui.util.ConstantUtil;

public class UserManageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1417380206977888383L;	
	private static final Logger log = Logger.getLogger(UserManageAction.class);
	private UserService userService;
	private String queryParam;
	// 分页
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;
	
	private Map<String, Object> resultMap;
	
	@SuppressWarnings("unchecked")
	public String individualClientBrowser(){
		resultMap = new HashMap();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			resultMap = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		Integer count = userService.getUserCountByConditions(resultMap);
		resultMap = queryMapParamUtil(resultMap);
		List<Map> companys = userService.getAllUserByConditions(resultMap);		
		resultMap.clear();
		resultMap.put(ConstantParamEnum.TOTALPROPERTY.getType(), count);
		resultMap.put(ConstantParamEnum.ROOT.getType(), companys);
		return SUCCESS;
	}

	private Map<String,Object> queryMapParamUtil(Map<String,Object> map) {		
		map.put("start",start ==null ? 0: start);
		map.put("pageSize",limit == null ? 20: limit);
		map.put("orderBy", sort == null ? "gmtLastLogin":sort);		
		if (dir != null){
			map.put("desc", dir.equals(ConstantUtil.DESC) ? true : false);			
		}else{
			map.put("desc",true);	
			
		}		
		return map;
	}
	
	public String sendActiveEmail(){
		resultMap = new HashMap<String, Object>();
		try {
			User user = userService.getUserByEmail(queryParam);
			userService.sendActiveEmail(user);			
			resultMap.put("msg", "发送激活邮件成功!");
		} catch (Exception e) {
		    log.error("发送激活邮件失败....", e);		   
			resultMap.put("msg", "发送激活邮件失败!");
		}
		return SUCCESS;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}
}
